<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	编辑商品
*/
class get_order extends \plugin{
	public function getdata($rqdata=array(),$backurl='',$pluginname=''){
		$this->model('member')->checklogin();
		$this->session('teamid',intval($rqdata['teamid']));
		$this->session('plugin_name',$rqdata['plugin_name']);
		$shopset = $this->plugin_setting('shopset');
		//营销设置
		$saleset=$this->plugin_setting('saleset');
		$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2','mobile','realname'));
		$member=array_merge($member,$member2);
		$goodsid = intval($rqdata['goodsid']);
		//来自商户的订单
		$merchid = intval($rqdata['merchid'])>0?intval($rqdata['merchid']):0;
		if($rqdata['storeid']){
			$_SESSION['store_id']=intval($rqdata['storeid']);
		}
		$store_id=$_SESSION['store_id'];
		$sendtype=intval($rqdata['sendtype']);
		//判断是不是点餐
		$_SESSION['is_dish']=intval($rqdata['is_dish']);
		//下单页
		$item=array();
		//提交数据
		$return['post'] = array();
		if($merchid>0){
			$return['merch'] = pdo_get('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$merchid));
		}else{
			$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
			if($carrier_list){
				foreach($carrier_list as &$pv){
					$pv['pcd']=json_decode($pv['pcd'],true);
				}
				$return['carrier_list']=$carrier_list;
				$return['selectstore']=$carrier_list[0];
			}
		}
		if($goodsid){
			$_SESSION['goodsid']=$goodsid;
			//直接购买。
			$return['post']['goodsid'] = $goodsid;
			$goodsfile=array('id','fang_credit2','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','ccate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname','api_data');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),$goodsfile);
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
			$goods['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),'weight');
			if($_SESSION['is_tomedia']){
				$goods['image']=$goods['image']?tomedia($goods['image']):'';
			}
			$diyfields=$this->model('goods')->get_diyformfields($goodsid);
			//配送设置
			$goodsdispatch=$this->model('goods')->get_dispatch($goodsid);
			$goods=array_merge($goods,$goodsdispatch);
			//如果来自其他商户分销（修改商户ID）
			$frommerchid=$this->session('frommerchid');
			$goods['merchid']=$frommerchid?$frommerchid:$goods['merchid'];
			//多商户商品：
			if($goods['merchid'] && !$merchid){
				$merchid=$goods['merchid'];
				$return['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['merchid']));
			}
			$goods['goodsid']=$goods['id'];
			$optionsid = intval($rqdata['optionsid']);
			$goods['storeid']=$store_id;
			$goods['optionid']=$optionsid;
			$goods['roomid']=intval($rqdata['roomid']);
			$goods=$this->model('activity')->get_upgoods($goods);
			if($goods['active']['dian_mid']){
					$goods['merchid']=pdo_getcolumn('jy_merch_user',array('mid'=>$goods['active']['dian_mid'],'uniacid'=>$_SESSION['uniacid']),'id');
					$merchid=$goods['merchid'];
			}
			//重置下单数量
			$goods['goodstotal']=$goods['total'];
			$goods['total']=intval($rqdata['num']);
			if($optionsid){
				$return['post']['optionid'] = $optionsid;
				if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
				}
				if(!$goods['options']['specs']){
					$optiontitle=$goods['options']['title'];
					$goods['istaocan'] = 1;
				}else{
					$specs = explode("_", $goods['options']['specs']);
					$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					if($specsall){
							$optiontitle='';
							foreach($specsall as $k2=>$sp){
								if($k2==0){
									$optiontitle .=$sp['title'];
								}else{
									$optiontitle .='，'.$sp['title'];
								}
							}
					}
				}
				$goods['optiondata']= $optiontitle;
				$goods['weight']=$goods['options']['weight']?:$goods['weight'];
			}else{
				$goods['optionid']=0;
				$return['post']['optionid'] = 0;
			}
			if($goods['active_plugin'] && !$optionsid){
				//商品在做活动，检查下库存是否不足
				$this->model('activity')->check_stock($goods['id'],$goods['active_plugin'],$goods['active_id'],$goods['total']);
			}
			$return['post']['total'] = intval($rqdata['num']);
			if($goods['type']==1 || $goods['type']==2){
				//虚拟商品
				$order['sendtype']=3;
			}elseif($goods['type']==3){
				//计时计次服务，发货类型必须是上门核销
				$order['sendtype']=2;
			}elseif($goods['type']==0 || $goods['isverify']){
				//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
				if(!$carrier_list || (($merchid<=0 || !$merchid) && !$goods['isverify'] && !$shopset['is_sendtype2'])){
					$order['sendtype']=1;
				}else{
					$return['sendtypes']=array(
						'1'=>'快递配送',
						'2'=>'上门自提',
					);
				}
			}
			$order=array(
				'sendtype'=>$order['sendtype'],
				'type'=>'one',
				'list'=>array($goods),
			);
		}else{
			//从购物车购买
			$order['sendtype']=1;//类型只能是快递配送
			if($rqdata['isbuyto']){
				$getordergoods=$this->model('cart')->getordergoods($merchid,$store_id,true);
			}else{
				$getordergoods=$this->model('cart')->getordergoods($merchid,$store_id,'',$pluginname);
			}
			$order['sendtype']=$getordergoods['sendtype']?:$order['sendtype'];
			$return['sendtypes']=$getordergoods['sendtypes']?:$return['sendtypes'];
			$diyfields=$getordergoods['diyfields'];
			$cart_list=$getordergoods['cart_list'];
			if(count($cart_list)>20){
				$order['need_cart_list']=1;
			}
			$order=array(
				'need_cart_list'=>$order['need_cart_list'],
				'sendtype'=>$order['sendtype'],
				'type'=>'cart',
				'list'=>$cart_list,
			);
		}
		$return['citypeisong']=$this->plugin_setting('citypeisong');
		if($return['citypeisong']['isopen']){
			$order['ismake']=$return['citypeisong']['ismake'];
		}
		if($merchid){
			$order['merchid']=$merchid;
			if(empty($return['merch'])){
				$return['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
			}
			$order['ismake']=$return['merch']['ismake'];
			if($return['merch']['sendtype']){
				$sendtype=$return['merch']['sendtype'];
			}
		}
		//点餐机制：
		$order['sendtype']=$sendtype?:$order['sendtype'];
		if($_SESSION['table_id']){
			$order['sendtype']=2;
		}
		if(empty($order['list'])){
			exi('请先选择商品再下单！','error',$backurl?:'jy_weishop/member.cart/');
		}
		//统计订单商品总数和总价
		$total=0;
		$total_price=0.00;
		$oldfee=0.00;
		$total_weight=0;
		$buyxiu_de_price=0;
		$fang_credit2=0;
		foreach($order['list'] as &$ov){
			if($ov['merchid']){
				$isid=pdo_getcolumn('jy_merch_user',array('id'=>$ov['merchid'],'uniacid'=>$_SESSION['uniacid']),'id');
				if(!$isid){
					$order['exitnomerch']=1;
				}
			}else{
				$order['exitnomerch']=1;
			}
			//返现
			if($ov['fang_credit2']){
				if(substr($ov['fang_credit2'],-1)=='%'){
					$ov['fang_credit2']=($ov['total']*$ov['price']*substr($ov['fang_credit2'],0,-1))/100;
				}else{
					$ov['fang_credit2']=$ov['total']*$ov['fang_credit2'];
				}
			}
			$total+=$ov['total'];
			$fang_credit2+=$ov['fang_credit2'];
			$total_price+=$ov['total']*$ov['price'];
			$oldfee+=$ov['total']*$ov['originalprice'];
			$total_weight+=$ov['weight']*$ov['total'];
			$buyxiu_de_price+=$ov['buyxiu_de_price'];
			if($ov['store_isopen']){
				$return['store_isopen']=1;
			}
			if($ov['api_data']){
				if(!is_array($ov['api_data'])){
					$ov['api_data']=json_decode($ov['api_data'],true);
				}
				if($ov['api_data']['no_paytype']){
					$return['post']['setno_paytype']=str_array($ov['api_data']['no_paytype']);
				}
			}
		}
		$order['fang_credit2']=$fang_credit2;
		$order['total']=$total;
		$order['total_price']=$total_price;
		$order['oldfee']=$oldfee;
		$order['total_weight']=round(($total_weight/1000)*1000,2);
		$order['buyxiu_de_price']=$buyxiu_de_price;
		//计算运费
		$item['address']=array();
		$order['dispatchprice']=0;
		$order['sendtype']=$order['sendtype']?:1;
		//刷新运费
		if(!$order['sendtype'] || $order['sendtype']==1 || $order['sendtype']==4 || $return['sendtypes']){
			$up_dispatch=$this->model('order/dispatch')->up_dispatch($order,$item,$rqdata['addressid'],$_SESSION['store_id'],$return['merch']);
			$order=$up_dispatch['order']?:$order;
			$item=$up_dispatch['item']?:$item;
		}
		$kuaipeisong=$this->plugin_setting('kuaipeisong');
		if($kuaipeisong['isnoopen']){
			$order['sendtype']=$order['sendtype']==1?2:$order['sendtype'];
			unset($return['sendtypes'][1]);
		}
		if($order['is_peisong']){
			$return['sendtypes']=array(
					'1'=>'同城配送',
					'2'=>'到店自取',
			);
		}
		if($_SESSION['is_dish']){
			$return['sendtypes']=array(
				'1'=>'外卖配送',
				'2'=>'到店自取',
			);
		}
		if($order['merchid']){
			$getsendtypes=model('jy_merch/model')->sendtypes($order['merchid'],$return['sendtypes'],true,$order['is_peisong']);
			$return['sendtypes']=$getsendtypes['sendtypes'];
			$order['ismake']=$getsendtypes['ismake'];
			$order['sendtype']=$getsendtypes['default'];
			$order['is_peisong']=$getsendtypes['is_peisong'];
		}
		if(count($return['sendtypes'])==1){
			$order['sendtype']=$return['sendtypes'][1]?1:2;
		}
		$return['sendtypes']=$return['sendtypes']?:array();
		if($order['total_price']>0){
			//会员折扣价
			$order['level_de_price']=$this->model('activity')->get_level_de_price($order,1);
			//运势折扣价
			$order['luck_de_price']=$this->model('activity')->get_luck_de_price($order);
			//满减优惠价格：
			$order['sale_enough']=$this->model('activity')->get_enough_de_price($order,$saleset);
			$order['enough_de_price']=$order['sale_enough'][0]['de_price']?$order['sale_enough'][0]['de_price']:0.00;
			//送积分
			$order['credit']=$this->model('activity')->get_buy_credit_order($order,'',$order['merchid']);
			$order['credit3']=$this->model('activity')->get_buy_credit3_order($order,'','',$order['merchid']);
			//积分抵扣
			$deduct_res=$this->model('activity')->get_deduct_credit_order($order,$saleset,$member);
			$order['deduct_credit']=$deduct_res['deduct_credit'];
			$order['delcredit1']=$deduct_res['delcredit1'];
			//优惠券优化价格：
			$order['coupon_de_price']=0.00;
			if(!$order['no_coupon_discount']){
				//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
				$newcoupons=$this->model('coupon')->getmycoupon_ing(1,$order['total_price'],$order['level_de_price'],$order['list'],$order['enough_de_price'],'','',$pluginname);
				$order['coupon_de_price']=$newcoupons[0]['de_price'];
				$item['couponid']=$newcoupons[0]['id'];
				$return['couponcount']=count($newcoupons);
			}
			$return['coupons']=$newcoupons?$newcoupons:array();
		}
		//整理收货地址信息
		if($store_id){
			$selectstore=pdo_get('jy_weishop_store',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
			if(!$selectstore){
				$_SESSION['store_id']=$return['selectstore']['id'];
			}else{
				$return['selectstore']=$selectstore;
			}
			if(!$return['selectstore']){
				$store_id=0;	
			}
		}
		if($return['selectstore']['id']){
			$return['selectstore']['juli']=1;
			$return['selectstore']['pcd']=json_decode($return['selectstore']['pcd'],true);
		}
		if($return['shopset']['addressdiyform']){
			$addressdiyform=pdo_getcolumn('jy_weishop_diyform_temp',array('id'=>$return['shopset']['addressdiyform'],'uniacid'=>$_SESSION['uniacid']),'diyformfields');
			$addressdiyform=$addressdiyform?json_decode($addressdiyform,true):array();
		}
		$return['address_list']=pdo_getall('core_address',array('isdel'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		foreach($return['address_list'] as &$av){
			$av['pcd']=json_decode($av['pcd'],true);
			$av['diyfields']=$av['diyfields']?json_decode($av['diyfields'],true):$addressdiyform;
		}
		$return['address_item']=array();
		$return['addressdiyform']=$addressdiyform;
		//提交数据
		$return['post']['is_peisong'] = $order['is_peisong'];
		$return['post']['invoice'] = $return['shopset']['invoice'];
		$return['post']['address'] = $item['address']?:array('city'=>'');
		$return['post']['merchid'] = $merchid;
		$return['post']['couponid'] = $newcoupons[0]['id'];
		$return['post']['make_type'] = 0;
		$return['post']['dayid'] = 0;
		$return['post']['title5'] = $newcoupons[0]['coupon']['title5'];
		$return['post']['deduct'] = $newcoupons[0]['coupon']['deduct'];
		$return['post']['discount'] = $newcoupons[0]['coupon']['discount'];
		$return['post']['backtype'] = $newcoupons[0]['coupon']['backtype'];
		$return['post']['addressid'] = $item['addressid'];
		$return['post']['sendtype'] = $order['sendtype']?:1;
		$return['post']['sendtime'] = strtotime("+2 day");
		$return['post']['storeid'] = $return['selectstore']['id'];
		$return['post']['is_work_time'] = $return['selectstore']['is_work_time'];
		$return['post']['deliverytime'] = date("m月d日",strtotime("+2 day"));
		$return['post']['diyfields'] = $diyfields;
		$return['post']['dispatchprice']=$order['dispatchprice'];
		$return['post']['realname']=$return['post']['address']['name']?:$member['realname'];
		$return['post']['mobile']=$return['post']['address']['mobile']?:$member['mobile'];
		if($_SESSION['uid'] && (!$return['post']['realname'] || !$return['post']['mobile'])){
			$lastorder=pdo_get('jy_weishop_order',array('mid'=>$_SESSION['uid'],'realname <>'=>'','mobile <>'=>''),array('realname','mobile'));
			$return['post']['realname']=$lastorder['realname'];
			$return['post']['mobile']=$lastorder['mobile'];
		}
		//配送
		$order['deliverytime'] = $return['post']['deliverytime'];
		//打包费
		$order['out_upfee']=$return['merch']['out_upfee']>0?$return['merch']['out_upfee']:0;
		//整理最终价(暂不扣除优惠券)post是最终价，order是未减优惠券的价
		$order['endprice']=round($order['total_price'] + $order['dispatchprice'] + $order['out_upfee'] - $order['luck_de_price']-$order['buyxiu_de_price']  - $order['enough_de_price'] - $order['deduct_credit'],2);
		$order['no_dispatch_price']=round($order['total_price'] + $order['out_upfee'] - $order['luck_de_price']-$order['buyxiu_de_price'] - $order['enough_de_price'] - $order['deduct_credit'],2);
		$order['endprice']=$order['endprice']>=0?$order['endprice']:0;
		$return['post']['endprice']=$order['endprice']- $order['coupon_de_price'];
		$return['post']['endprice']=$return['post']['endprice']>=0?$return['post']['endprice']:0;
		$return['post']['no_dispatch_price']=$order['no_dispatch_price']- $order['coupon_de_price'];
		$order['no_dispatch_price2']=$return['post']['no_dispatch_price'];
		$return['post']['no_dispatch_price']=$return['post']['no_dispatch_price']>=0?$return['post']['no_dispatch_price']:0;
		$order['no_dicard_endprice']=$return['post']['endprice'];
		$order['no_dicard_dispatch_endprice']=$return['post']['no_dispatch_price'];
		//购物卡抵扣
		$last_cardfee=$this->model('goodscard')->getorderfee($order['total_price'],$order['level_de_price'],$order['list'],$order['enough_de_price']);
		if($last_cardfee>0){
			$order['last_cardfee']=$last_cardfee;
			if($order['last_cardfee']<=$return['post']['endprice']){
				$order['di_cardfee']=$order['last_cardfee'];
			}else{
				$order['di_cardfee']=$return['post']['endprice'];
			}
			$return['post']['endprice'] -=$order['di_cardfee'];
			if($order['last_cardfee']<=$return['post']['no_dispatch_price']){
				$order['no_dispatch_cardfee']=$order['last_cardfee'];
			}else{
				$order['no_dispatch_cardfee']=$return['post']['no_dispatch_price'];
			}
			$return['post']['no_dispatch_price'] -=$order['no_dispatch_cardfee'];
		}
		
		/*
			赠品
			先根据订单金额找赠品，再根据商品找赠品
		*/
		$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
		//$current_gift=array();
		if($goods_gifts){
			foreach($goods_gifts as $gift){
				$gift['onlinetime']=json_decode($gift['onlinetime'],true);
				if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['orderprice'] && $gift['orderprice']<$order['no_dispatch_price']){
					if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
						$current_gift=$gift;
					}
				}
			}
		}
		if(!$current_gift){
			$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
		}
		if($goods_gifts){
			foreach($goods_gifts as $gift){
				$gift['onlinetime']=json_decode($gift['onlinetime'],true);
				$gift['goodsid']=json_decode($gift['goodsid'],true);
				if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['goodsid']){
					foreach($order['list'] as $goods){
						foreach($gift['goodsid'] as $gv){
							if($goods['goodsid']==$gv['id']){
								$current_gift=$gift;
								break;
							}
						}
					}
				}
				if($current_gift){
					break;
				}
			}
		}
		if($current_gift){
			$order['gift_id']=$current_gift['id'];
			$return['gifts']=json_decode($current_gift['gifts'],true);
			foreach($return['gifts'] as &$gv){
				$gv['price']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),'price');
				if($_SESSION['is_tomedia']){
					$gv['image']=$gv['image']?tomedia($gv['image']):'';
				}
			}
		}
		$order['total_price']=round($order['total_price'],2);
		$return['order']=$order;
		if($order['need_cart_list']){
			unset($order['list']);
		}
		$_SESSION['order']=$order;
		$item['sendtype']=1;
		$return['item']=$item;
		$return['member']=$member;
		//自提点
		$return['hours']=array('01','');
		//代付
		$peerpay_setting=$this->plugin_setting('peerpay');
		if($peerpay_setting && $peerpay_setting['open'] && $order['endprice']>0){
			$return['peerpay']=true;
		}
		//获取小程序订阅消息ID
		$tmplIds=array();
		$return['tmplIds']=array();
		$return['fromh5wxapp']=$_SESSION['fromh5wxapp'];
		$return['openid']=$_SESSION['openid'];
		$storeset=$this->plugin_setting('storeset');
		if($storeset['open_storeid'] && !$return['post']['merchid']){
			$return['showstore_no']=1;
			$return['store_notitle']=$storeset['store_notitle']?:'门店编号';
		}
		$return['no_storechange']=$storeset['no_change'];
		//小程序自定义表单用到
		$return['nowday']=date('Y-m-d');
		$return['nowmonthday']=date('m-d');
		$return['nowtime']=time();
		$return['mid'] = intval($_SESSION['uid']);
		$return['buyxieyi']=plugin('jy_weishop')->plugin_setting('xieyi');
		if($this->plugin_exists('jy_aiaddress')){
			$return['jy_aiaddress']=plugin('jy_aiaddress')->plugin_setting('basic');
		}
		if($this->plugin_exists('jy_yuejie')){
			$return['yuejiemid']=model('jy_yuejie/model')->checktype();
		}
		return $return;
	}
	public function new_order($post=array()){
		$order=$_SESSION['order'];
		if(empty($order)){
			exi('订单异常，请刷新页面！','error');
		}
		if($post['storeid']){
			$_SESSION['store_id']=$post['storeid'];
			$order['storeid']=$post['storeid'];
			//自提点（自提）
			if($post['sendtype']==4){
				$selectstore=pdo_get('jy_weishop_store',array('status'=>1,'type'=>array(1,3),'uniacid'=>$_SESSION['uniacid'],'id'=>$post['storeid']),array('address','pcd'));
				$post['address']=array(
					'name'=>$post['realname'],
					'mobile'=>$post['mobile'],
					'address'=>$selectstore['address'],
					'pcd'=>json_decode($selectstore['pcd'],true),
				);
			}
		}
		if($order['need_cart_list']){
			$getordergoods=$this->model('cart')->getordergoods($order['merchid'],$_SESSION['store_id']);
			$order['list']=$getordergoods['cart_list'];
		}
		$this->model('goods')->ordercheck_buy($order,$post);
		if(!$post['merchid'] && $post['sendtype']==2 && $post['is_work_time'] && empty($post['tostoreday']) && !$post['plugin_name']){
			if(!$post['tostoretime']){
				//exi('请选择到店时间！','error');
			}
			if(!$post['tostoreday']){
				$y=date("Y");
				$post['tostoreday']=$y.'-'.$post['tostoretime'];
				$tostoredaytime=strtotime($post['tostoreday']);
				if($tostoredaytime<time()){
					$y++;
					$post['tostoreday']=$y.'-'.$post['tostoretime'];
				}
			}
		}
		//公共参数
		if($post['out_time'] && $post['make_type']){
			//配送时间
			$days = array(
					0=>date("Y-m-d"),
					1=>date("Y-m-d",strtotime("+1 day")),
					2=>date("Y-m-d",strtotime("+2 day")), 
					3=>date("Y-m-d",strtotime("+3 day")), 
					4=>date("Y-m-d",strtotime("+4 day")),
					5=>date("Y-m-d",strtotime("+5 day")),
					6=>date("Y-m-d",strtotime("+6 day")),
			);
			$dayid=intval($post['dayid']);
			$post['postout_time']=$post['out_time'];
			$post['daytime']=$days[$dayid];
			$post['out_time']=$days[$dayid].' '.$post['out_time'];
		}
		foreach($post['diyfields'] as &$dv){
			if(is_array($dv['data'])){
				$data=';';
				foreach($dv['data'] as $vv){
					$data .=$vv.';';
				}
				$dv['data']=$data;
			}
		}
		$new_order=array(
			'mid'=>$_SESSION['uid'],
			'goodsprice'=>$order['total_price'],
			'oldfee'=>$order['oldfee'],
			'remark'=>trim($post['remark']),
			'goods'=>$order['list'],
			'merchid'=>$order['merchid'],
			'sendtype'=>intval($post['sendtype'])?:1,
			'diyfields'=>ijson_encode($post['diyfields']),
			'out_time'=>trim($post['out_time']),
			'tostoretime'=>trim($post['tostoreday']).' '.trim($post['tostorehour']),
			'make_type'=>intval($post['make_type']),
			'is_peisong'=>$post['is_peisong'],
			'is_dingpei'=>$post['is_dingpei'],
			'out_upfee'=>$order['out_upfee']?:0,
			'mobile'=>$post['mobile']?:$post['address']['mobile'],
			'realname'=>$post['realname']?:$post['address']['name'],
			'address'=>$post['address'],
			'dispatchid'=>intval($post['dispatchid']),
			'buytype'=>intval($post['buytype']),
		);
		if($post['tostorehour']){
			$tostoretimehour=trim($post['tostoreday']).' '.substr($post['tostorehour'],0,5);
			$tostoretime=strtotime($tostoretimehour);
			if($tostoretime<time()){
				exi('到店时间段已过！请选择其他时间！','error');
			}
		}
		$new_order['api_data']=array();
		$new_order['invoice']=array(
			'type'=>$post['invoicetype'],
			'goodstype'=>$post['invoicegoodstype'],
			'toutype'=>$post['fapiaotype2'],
			'name'=>$post['invoicename'],
			'code'=>$post['invoicecode'],
			'email'=>$post['email'],
			'bank'=>$post['bank'],
			'bankcode'=>$post['bankcode'],
		);
		//会员折扣
		if(!$order['no_member_discount'] && $order['level_de_price']){
			$new_order['level_de_price']=$order['level_de_price'];
		}
		//运势折扣
		if($order['luck_de_price']){
			$new_order['luck_de_price']=$order['luck_de_price'];
		}
		//买家秀
		if($order['buyxiu_de_price']){
			$new_order['buyxiu_de_price']=$order['buyxiu_de_price'];
		}
		//满减
		$new_order['enough_de_price']=$order['enough_de_price'];
		//抵扣
		$new_order['deduct_credit']=$order['deduct_credit']?:0;
		$new_order['delcredit1']=$order['delcredit1']?:0;
		//优惠券
		$new_order['price']=$new_order['goodsprice']+$new_order['out_upfee']-$new_order['enough_de_price']-$new_order['level_de_price']-$new_order['luck_de_price']-$new_order['buyxiu_de_price'];
		if(!$post['no_deduct_credit']){
			$new_order['price'] -=$new_order['deduct_credit'];
		}else{
			$new_order['deduct_credit']=0;
			$new_order['delcredit1']=0;
		}
		if(!$order['no_coupon_discount']){
			$couponid=intval($post['couponid']);
			//获取优惠信息
			$getcouponfee=$this->model('coupon')->getorderfee($couponid,$new_order['price'],$new_order['goodsprice']);
			if($getcouponfee){
				$new_order=array_merge($new_order,$getcouponfee);
			}
		}
		//自动分配门店
		$storeset=$this->plugin_setting('storeset');
		if($storeset['open_storeid'] && !$post['merchid'] && !$post['store_no']){
			$store_notitle=$storeset['store_notitle']?:'门店编号';
			exi('请输入'.$store_notitle.'！','error');
		}
		if($post['store_no'] && $post['store_no']!=888888 && $post['store_no']!=$storeset['store_no']){
			$post['storeid']=$this->model('store')->getno_storeid($post['store_no']);
		}
		if($storeset['open_storeid'] && ($post['store_no']==888888 || $post['store_no']==$storeset['store_no'])){
			$post['storeid']=0;
		}
		$new_order['storeid']=intval($post['storeid']);
		if($new_order['sendtype']==1){
			$citypeisong=$this->plugin_setting('citypeisong');
			if($citypeisong['isopen'] && !$post['dayid'] && !$new_order['is_dingpei'] && $post['is_peisong']==1){
				$daytime=$post['daytime']?:date("Y-m-d");
				if($citypeisong['set_timebox']){
					$dayymd=date("Y-m-d");
					$start=0;
					$end=0;
					foreach($citypeisong['peisong_timebox'] as $pv){
							if($pv['times']['start']){
								$starttime=strtotime($dayymd.' '.$pv['times']['start']);
								if($starttime<$start || !$start){
									$start=$starttime;
								}
							}
							if($pv['times']['end']){
								$endtime=strtotime($dayymd.' '.$pv['times']['end']);
								if($endtime>$end){
									$end=$endtime;
								}
							}
					}
					$onlinetime=array();
				}elseif($new_order['storeid']){
					$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$new_order['storeid']));
					$onlinetime=json_decode($store['onlinetime'],true);
				}else{
					$onlinetime=array('start'=>'8:00','end'=>'20:00');
				}
				$daytime=$post['daytime']?:date("Y-m-d");
				if($onlinetime['start']){
					$start=$daytime.' '.$onlinetime['start'];
					$start=strtotime($start);
				}
				if($onlinetime['end']){
					$end=$daytime.' '.$onlinetime['end'];
					$end=strtotime($end);
				}
				//结束时间为凌晨
				if($end<$start){
					$end +=24*3600;
				}
				if(!$post['postout_time'] && $post['is_peisong']){
					$out_timestart=time();
					if($out_timestart>$end && $end){
						exi('超出配送时间！请选择预约配送!','error');
					}
					if($out_timestart<$start && $start){
						exi('超出配送时间！请选择预约配送','error');
					}
				}
				if($post['postout_time'] && $start && $end){
					$postout_time=str_array($post['postout_time'],'~');
					$out_timestart=$daytime.' '.$postout_time[0];
					$out_timestart=strtotime($out_timestart);
					if($out_timestart>$end && $end){
						exi('超出配送时间！','error');
					}
					$out_timeend=$daytime.' '.$postout_time[1];
					$out_timeend=strtotime($out_timeend);
					if($out_timeend && $out_timeend<$start && $start){
						exi('超出配送时间！请选择其他时间段','error');
					}
				}
			}
			//快递配送
			$new_order['addressid']=intval($post['addressid']);
			if(!$new_order['addressid'] && $_SESSION['uid'] && !$new_order['address']['address']){
				exi('请先选择收货地址！','error');
			}
			if($new_order['addressid']){
				$new_order['address']=pdo_get('core_address',array('id'=>$new_order['addressid']));
				$new_order['address']['pcd']=json_decode($new_order['address']['pcd'],true);
			}
			$new_order['mobile']=$new_order['mobile']?:$new_order['address']['mobile'];
			$pcd=$new_order['address']['pcd'];
			if($pcd){
				$pcd=json_decode($pcd,true);
				$new_order['dispatchprice']=$post['dispatchprice'];
				if(!$post['isbaoyou']){
					$new_order['price']+=$new_order['dispatchprice'];
				}else{
					$new_order['api_data']['isbaoyoufee']=$new_order['dispatchprice'];
					$new_order['dispatchprice']=0;
				}
			}		
			if($post['juli']){
				$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
				$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
				if($l['lng'] && $l['lat']){
					$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
					if($d/1000>$post['juli']){
						exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
					}
				}
			}		
		}elseif($new_order['sendtype']==2 || $new_order['sendtype']==4){
			//门店自提
			if(!$new_order['storeid'] && !$order['merchid'] && !$post['area_dianid']){
				exi('请先选择门店或自提点！','error');
			}
			$shopset = $this->plugin_setting('shopset');
			if((!$new_order['realname'] || !$new_order['mobile']) && $shopset['is_noordermobile']){
				exi('请输入联系人和电话！','error');
			}
			if(strlen($new_order['mobile'])!=11 || $new_order['mobile'][0]!='1' || !is_numeric($new_order['mobile'])){
				exi('手机号码格式错误，请输入11位，1开头的手机号！！','error');
			}
		}elseif($new_order['sendtype']==3 && !$_SESSION['wxapp_id']){
			//虚拟发货
			$shopset = $this->plugin_setting('shopset');
			if((!$new_order['realname'] || !$new_order['mobile']) && $shopset['is_noordermobile']){
				exi('请输入联系人和电话！','error');
			}
			if(strlen($new_order['mobile'])!=11 || $new_order['mobile'][0]!='1' || !is_numeric($new_order['mobile'])){
				exi('手机号码格式错误，请输入11位，1开头的手机号！！','error');
			}
		}
		//购物卡抵扣
		if(!$post['no_di_cardfee'] && $order['di_cardfee']>0){
			$last_cardfee=$this->model('goodscard')->getorderfee($order['total_price'],$order['level_de_price'],$order['list'],$order['enough_de_price']);
			if($last_cardfee>0){
				if($last_cardfee<=$new_order['price']){
					$new_order['di_cardfee']=$last_cardfee;
				}else{
					$new_order['di_cardfee']=$new_order['price'];
				}
				$new_order['price'] -=$new_order['di_cardfee'];
			}
		}
		$new_order['price']=$new_order['price']>0?$new_order['price']:0;
		if($new_order['price']<0){
			exi('订单金额不能小于零！','error');
		}
		if($order['gift_id']){
			$new_order['gift_id']=$order['gift_id'];
		}
		//餐桌
		$new_order['tableid']=$order['tableid'];
		if($new_order['tableid'] && $this->session('usernum')){
			$new_order['api_data']['usernum']=$this->session('usernum');
			$new_order['api_data']['childnum']=$this->session('childnum');
		}
		if($_SESSION['is_dish']){
			$new_order['isdish']=1;
		}
		//如果需要制作，配送状态为未制作！
		if($order['ismake']==1 || $post['buytype']==4){
			$new_order['send_status']=11;
		}
		//拼单部分
		if($this->session('teamid')){
			$new_order['teamid']=$teamid;
			$new_order['is_team']=1;
			$new_order['plugin_name']=$this->session('plugin_name');
		}
		$new_order['api_data']['peisong_juli']=$post['peisong_juli'];
		unset($_SESSION['order']);
		return $new_order;
	}
	public function getlist($params=array(),$rqdata=array()){
		$shopset = $this->plugin_setting('shopset');
		if($_SESSION['uid'] && !$params['mid']){
			$params['mid']=$_SESSION['uid'];
		}elseif($rqdata['mobile']){
			$params['mobile']=trim($rqdata['mobile']);
			$params['mid']=0;
		}else{
			$params['deleted']='nodata';
		}
		if(isset($rqdata['status']) && $rqdata['status'] !=100){
			$params['status']=intval($rqdata['status']);
		}
		if(!empty($rqdata['ordersn'])){
			$params['ordersn']=intval($rqdata['ordersn']);
		}
		if(!empty($rqdata['userdeleted'])){
			$params['userdeleted']=1;
		}
		if(!empty($rqdata['title'])){
			$return['params']['title']=$rqdata['title'];
			$goodsparams=array('uniacid'=>$_SESSION['uniacid']);
			$goodsparams['title like']='%'.trim($rqdata['title']).'%';
			$goodsids=pdo_getall('jy_weishop_goods',$goodsparams,'id','id');
			if($goodsids){
				$goodsids=array_keys($goodsids);
				$ids=pdo_getall('jy_weishop_order_goods',array('goodsid'=>$goodsids,'uniacid'=>$_SESSION['uniacid']),'orderid','orderid');
				if($ids){
					$ids=array_keys($ids);
					$params['id']=$ids;
				}else{
					$params['id']=0;
				}
			}else{
				$params['id']=0;
			}
		}
		$pindex = max(1, intval($rqdata['page']));
		$psize = 10;
		$total='';
		$fields=array('id','price','addprice','ispayadd','createtime','ispay','mid','paytime','status','send_status','buytype','sendtype','ordersn','addressid','dispatchprice','yuejiemid','paytype','pay_id','creditfee');
		if($rqdata['api_data']){
			$fields[]='api_data';
		}
		$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
		$feemoneyset=$this->plugin_setting('feemoney');
		$feemoneyset['feerade']=$feemoneyset['feerade']?:5;
		$pay_types=get_pay_types();
		foreach($list as &$item){
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),array('id','status','goodsid','optionid','price','total','goodssn','productsn','istaocan','goodsnum','optiondata'));
			$merchs =array();
			$item['feemoney']=0;
			foreach($ordergoods as &$gv){
				$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
				$goodsinfo['ordergoodsid']=$gv['id'];
				$goodsinfo['refund_id']=$this->pdo_getcolumn('order_refund',array('ordersn'=>$item['ordersn'],'ordergoodsid'=>$gv['id']),'id');
				$goodsinfo['can_refund']=$this->model('order/refund')->check_order_rerefund($item['id'],'',$gv['goodsid'],$gv['id']);
				$goodsinfo['total']=$gv['total'];
				$goodsinfo['price']=$gv['price'];
				$goodsinfo['goodsnum']=$gv['goodsnum'];
				if($_SESSION['is_tomedia']){
					$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
				}
				$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'weight');
				if($feemoneyset['isopen']==1){
					$gv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$item['price']:$feemoneyset['feerade']*$item['price']/100;
					$gv['feemoney']=$gv['total']*$gv['feemoney'];
					$item['feemoney'] +=$gv['feemoney'];
					$goodsinfo['feemoney']=price($gv['feemoney']);
				}
				//获取门店
				$stores = array();
				$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
				if ($goodsinfo['isverify'] == 1) {
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
						}
					}
				}elseif($shopset['openstore'] ==1){
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
						}
					}
				}
				$goodsinfo['store']=$stores;
				//商品规格
				if($gv['optionid']){
					if($gv['optiondata']){
						$goodsinfo['selespename']=$gv['optiondata'];
					}else{
						$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$goodsinfo['selespename']=implode("，",$specs);
					}
				}
				//获取商户
				if($goodsinfo['merchid']){
					$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
					$merchs[$goodsinfo['merchid']] = $merchinfo;
					$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
				}else{
					$merchs[0]['goods'][] = $goodsinfo;
				}
			}
			$item['feemoney']=price($item['feemoney']);
			//获取地址
			$item['address']=json_decode($item['address'],true);
			if($item['address'] && $item['address']['name']){	
				$address=$item['address'];
			}else{
				$address=$item['addressid'];
			}
			$item['address']=model('core/member')->get_address_str($address);
			$item['pingjiatime'] = pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid'=>0),'createtime');
			if($item['pingjiatime']){
				$comment_changedays=$shopset['comment_changedays']?:0;
				$endpingjiatime = $item['pingjiatime'] + $comment_changedays*24*3600;
				if($endpingjiatime>TIMESTAMP){
					$item['pingjia']=1;//1修改评价；2追加评价
				}else{
					$zuipingjia = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid >'=>0),'id');
					if($zuipingjia<1){
						$item['pingjia']=2;
					}else{
						$item['pingjia']=3;
					}
				}
			}
			$item['merchs']=$merchs;
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['statustitle']=$this->order_status[$item['status']];
			$item['buytypetitle']=$this->buy_types[$item['buytype']];
			$item['paytypetitle']=$pay_types[$item['paytype']];
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
			$item['can_refund']=$this->model('order/refund')->check_order_rerefund($item['id']);
			$item['can_cancel']=$this->model('order/refund')->check_order_cancel($item);
			if($rqdata['api_data']){
				$item['api_data']=json_decode($item['api_data'],true);
			}
		}
		return $list;
	}
	public function orderitem($rqdata=array()){
		$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
		$shopset=plugin('jy_weishop')->plugin_setting('shopset');
		if(!$isfollow && $rqdata['fromcreate'] && !$shopset['noshowqrcode']){
			$return['showwechatcode']=1;
		}
		if($rqdata['id']){
			$id = intval($rqdata['id']);
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($rqdata['id'])));
		}elseif($rqdata['ordersn']){
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>trim($rqdata['ordersn'])));
		}
		if(!$order){
			exi('指定订单不存在！','error','goback');
		}
		if($order['mid']!=$_SESSION['uid']){
			exi('指定订单不是你的！','error','goback');
		}
		$order['api_data']=json_decode($order['api_data'],true);
		if($order['status']==0){
			$order['fromseconds']=time()-$order['createtime'];
			$order['outseconds']=$shopset['orderpay_outtime']*3600;
			if($order['fromseconds']<$order['outseconds']){
				$order['fromseconds']=($order['fromseconds']/$order['outseconds'])*428;
				$order['upseconds']=$this->model('model')->date($order['outseconds']-$order['fromseconds']);
			}else{
				$order['upseconds']=0;
			}
		}
		//定义订单状态名称
		if($order['status']==0 && $order['paytype']!='hdfk'){
			$order['statustitle']='订单待支付';
			$order['statusdesc']='超时未支付，订单将自动取消';
		}elseif($order['status']==1 && $order['paytype']=='hdfk'){
			$order['statustitle']='货到付款';
			$order['statusdesc']='请您收到商品后付款';
		}elseif($order['status']==1 && !$order['refundstate']){
			$order['statustitle']='买家已付款';
			$order['statusdesc']='请您留意订单进度';
		}elseif($order['status']==2 && !$order['refundstate']){
			$order['statustitle']='商家已发货';
			$order['statusdesc']='请您留意订单进度';
		}elseif($order['status']==3 && !$order['refundstate']){
			$order['statustitle']='订单已完成';
			$order['statusdesc']='欢迎下次光临！';
		}elseif($order['status']==4 && !$order['refundstate']){
			$order['statustitle']='订单已关闭';
			$order['statusdesc']='欢迎下次光临！';
		}elseif($order['status']==5){
			$order['statustitle']='申请退款中';
			$order['statusdesc']='请您留意退款进度';
		}elseif($order['status']==5 && $order['oldstatus']>1){
			$order['statustitle']='申请售后中';
			$order['statusdesc']='请您留意售后进度';
		}elseif($order['status']==6){
			$order['statustitle']='申请售后成功';
			$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
		}else{
			$order['statustitle']='订单已关闭';
			$order['statusdesc']='欢迎下次光临！';
		}
		//包含核销商品
		if($order['status']==2 && $order['isverify']==1){
			$order['statustitle']='部分商品已发货';
			$order['statusdesc']='除核销商品，其他商品已发货';
		}elseif($order['status']==2 && $order['isverify']==2){
			$order['statustitle']='等待核销';
			$order['statusdesc']='属核销商品，请核销！';
		}elseif($order['status']==3 && $order['isverify']==2){
			$order['statustitle']='已核销';
			$order['statusdesc']='属核销商品，已核销完成！';
		}
		$order['express']=pdo_get('jy_weishop_order_express',array('orderid'=>$order['id'],'opensend_type'=>0));
		$order['express']['express_name']=$this->model('model')->express_types()[$order['express']['express_id']]['name'];
		//时间格式化
		$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
		$order['paytime']=date('Y-m-d H:i:s',$order['paytime']);
		$order['sendtime']=$order['express']['createtime']?date('Y-m-d H:i:s',$order['express']['createtime']):'';
		$order['finishtime']=date('Y-m-d H:i:s',$order['finishtime']);
		if ($order['userdeleted'] == 2) {
				exi('订单已经被删除!','error');
		}
		//用户地址
		$order['address']=json_decode($order['address'],true);
		if($order['address'] && $order['address']['name']){
			$address=$order['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
		}
		if(!is_array($address['pcd'])){
			$address['pcd']=json_decode($address['pcd'],true);
		}
		$address['diyfields']=json_decode($address['diyfields'],true);
		$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));                       //用户信息
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','goodsnum','optiondata','price','total','goodssn','productsn'));
		$merchs =array();
		$jy_giftbao=$this->plugin_exists('jy_giftbao');
		foreach($ordergoods as &$gv){
				$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','unit','type','image','title','merchid'));
				if($jy_giftbao){
					$libaolog=pdo_get('jy_giftbao_active_log',array('goods_id'=>$gv['goodsid'],'ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
					if($libaolog['id']){
						$goodsinfo['libao']=pdo_get('jy_giftbao_active',array('id'=>$libaolog['active_id'],'uniacid'=>$_SESSION['uniacid']));
						$goodsinfo['libao']['log_id']=$libaolog['id'];
						$goodsinfo['libao']['libaos']=$goodsinfo['libao']['libaos']?json_decode($goodsinfo['libao']['libaos'],true):array();
						$libaolog['active_libaonum']=$goodsinfo['libao']['active_libaonum']=count($goodsinfo['libao']['libaos']);
						$libaolog['active_choosenum']=$goodsinfo['libao']['active_choosenum']=$goodsinfo['libao']['choosenum'];
						foreach($goodsinfo['libao']['libaos'] as &$glb){
							$glb['price']=pdo_getcolumn('jy_giftbao_libao',array('id'=>$glb['id']),'price');
						}
						if($libaolog['libaos']){
							$libaolog['libaos']=json_decode($libaolog['libaos'],true);
							$goodsinfo['libao']['libaos']=$libaolog['libaos'];
						}else{
							$libaolog['libaos']=array();
							$return['showchooselibaos']=true;
							$return['nowlibao']=$goodsinfo['libao'];
						}				
						foreach($libaolog['libaos'] as &$lb){
							$lb['price']=pdo_getcolumn('jy_giftbao_libao',array('id'=>$lb['id']),'price');
						}
					}
					$goodsinfo['libaolog']=$libaolog;
				}
				$goodsinfo['total']=$gv['total'];
				$goodsinfo['price']=$gv['price'];
				$goodsinfo['goodsnum']=$gv['goodsnum'];
				if($_SESSION['is_tomedia']){
					$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
				}
				$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
				$gv['title']=$goodsinfo['title'];
				$gv['image']=$goodsinfo['image'];
				$gv['merchid']=$goodsinfo['merchid'];	
				//获取门店
				$stores = array();
				$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
				if ($goodsinfo['isverify'] == 1) {
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
						}
					}
				}elseif($shopset['openstore'] ==1){
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
						}
					}
				}
				$goodsinfo['store']=$stores;
				//商品规格
				if($gv['optionid']){
					if($gv['optiondata']){
						$goodsinfo['selespename']=$gv['optiondata'];
					}else{
						$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$goodsinfo['selespename']=implode("，",$specs);
					}
				}
				$goodsinfo['buytypetitle']=$this->buytypes()[$order['buytype']];
				//获取商户
				if($goodsinfo['merchid']){
					if(!$merchs[$goodsinfo['merchid']]){
						$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
						$merchs[$goodsinfo['merchid']] = $merchinfo;
					}
					$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
					$return['ismerch']=1;
				}else{
					$merchs[0]['goods'][] = $goodsinfo;
				}
				if($order['storeid']){
					$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$order['storeid']));
				}
		}	
		$return['self']=pdo_get('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		$order['selfnumber']=$return['self']['number'];
		if($order['selfnumber']){
			$order['selfnumberurl']=custom_url('jy_weishop/verify.selfdetail//id='.$order['id'].'&verifycode='.$order['selfnumber'],true);
			//小程序二维码：
			$url='/jy_weishop/verify/selfdetail/index?id='.$order['id'].'&verifycode='.$order['selfnumber'];
			$pid='verifyid'.$order['id'].'c'.$order['selfnumber'];
			$order['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
		}
		if($order['sendtype']==1 || $order['sendtype']==4){
			if($order['peisong_id']){
				if(!$this->plugin_exists('jy_peisong')){
					exi('请联系管理员开通《配送员》插件！','error');
				}
				$return['peisong']=pdo_get('jy_peisong_member',array('mid'=>$order['peisong_id']));
			}else{
				$return['peisong']=pdo_get('jy_weishop_order_express',array('orderid'=>$order['id']));
			}
		}
		$return['merchs']=$merchs;
		//抵扣	
		$return['order_gifts']=pdo_get('jy_weishop_order_gift',array('order_id'=>$order['id']));
		if($return['order_gifts']){
			$return['order_gifts']['gifts']=json_decode($return['order_gifts']['gifts'],true);
			if($_SESSION['is_tomedia']){
			foreach($return['order_gifts']['gifts'] as &$giv){
				$giv['image']=$giv['image']?tomedia($giv['image']):'';
			}
			}
		}
		$stores_count=count($stores);
		if (!empty($order['diyformdata'])) {
			$order_data = $order['diyformdata'];
		}
		$order['can_refund']=$this->model('order/refund')->check_order_rerefund($order['id']);
		$order['can_cancel']=$this->model('order/refund')->check_order_cancel($order);
		$return['stores_count']=$stores_count;
		if($order['sendtype']==3 && $order['status']==3){
			$order['content']=pdo_getcolumn('jy_weishop_order_virtual',array('orderid'=>$order['id']),'content');
		}
		$order['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']),'total');
		//卡密
		if($order['status']>0 && $order['status']!=4){
			$return['virtual']=pdo_getall('jy_weishop_order_virtual',array('orderid'=>$order['id']));
		}
		if($return['virtual']){
			foreach($return['virtual'] as &$vv){
				$tempinfo=pdo_get('jy_weishop_virtual_temp',array('id'=>$vv['virtualid'],'uniacid'=>$_SESSION['uniacid']),array('id','linktitle','linkurl'));
				$vv['linkurl']=$tempinfo['linkurl'];
				$vv['linktitle']=$tempinfo['linktitle'];
			}
		}
		
		$return['address']=$address;
		$return['member']=$member;
		$return['goods']=$ordergoods;
		$return['stores']=$stores;
		$return['order_data']=json_decode($order_data,true);
		if($order['status']>0 && $order['status']!=4){
			$order_verify=pdo_getall('jy_weishop_order_verify',array('orderid'=>$order['id']));
		}
		if($order_verify){
				foreach($order_verify as &$verify){
					$verify['verifycode']=$verify['verify_code'];
					$code_endtime=$verify['endtime']?date('Y-m-d',$verify['endtime']):'';
					if($verify['status']==0 && $verify['endtime'] && $verify['endtime']<TIMESTAMP){
						$verify['status']=2;
						pdo_update('jy_weishop_order_verify',array('status'=>2),array('id'=>$verify['id']));
					}else{
						$verify['url']=custom_url('jy_weishop/verify.detail//id='.$order['id'].'&verifycode='.$verify['verify_code'],true);
						//小程序二维码：
						$url='/jy_weishop/verify/detail/index?id='.$order['id'].'&verifycode='.$verify['verify_code'];
						$pid='verifyid'.$order['id'].'c'.$verify['verify_code'];
						$verify['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
					}
				}
		}
		$return['order_verify']=$order_verify;
		if($order['status']>0 && $order['status']!=4){
			$goods_verify=pdo_getall('jy_weishop_verifygoods',array('orderid'=>$order['id']));
		}
		if($goods_verify){
				foreach($goods_verify as &$gvv){
					if (!(empty($gvv['limitnum']))){
						$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$gvv['id']));
						$verifynum = 0;
						foreach ($verifygoodlogs as &$verifygoodlog) 
						{
							$verifygoodlog['verifydate']=date('Y-m-d H:i:s',$verifygoodlog['verifydate']);
							$verifynum += intval($verifygoodlog['verifynum']);
						}
						$gvv['verifygoodlogs']=$verifygoodlogs;
						$gvv['lastverifys'] = intval($gvv['limitnum']) - $verifynum;
					}
					if (empty($gvv['limittype'])) {
							$gvv['limitdays']=$gvv['limitdays']?:365;
							$limitdate = intval($gvv['starttime']) + (intval($gvv['limitdays']) * 86400);
					}else{
							$limitdate = intval($gvv['limitdate']);
					}
					if ($limitdate < time()) {
							$gvv['status']=3;
					}
					$code_endtime=$limitdate?date('Y-m-d',$limitdate):'';
					$gvv['url']=custom_url('jy_weishop/verify.verifygoodsdetail//id='.$order['id'].'&verifycode='.$gvv['verifycode'],true);
					//小程序二维码：
					if($_SESSION['fromh5wxapp']){
						//不能超128字符
						$url='/jy_weishop/verify/verifygoodsdetail/index?id='.$order['id'].'&verifycode='.$gvv['verifycode'];
						$url=urlencode($url);
						$url='/pages/html/html?url='.$url;
					}else{
						$url='/jy_weishop/verify/verifygoodsdetail/index?id='.$order['id'].'&verifycode='.$gvv['verifycode'];
					}
					$pid='verifygoodsid'.$order['id'].'c'.$gvv['verifycode'];
					$gvv['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
				}
		}
		$return['goods_verify']=$goods_verify;
		$return['code_endtime']=$code_endtime;
		if(count($return['virtual']) || count($return['goods_verify']) || count($return['order_verify'])){
			$order['isverify']=2;
		}
		$order['paylog']=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
		if($order['paylog']['di_fee']>0){
			$credit_types=credit_types(1,1);
			$order['paylog']['difeetitle']=$credit_types[$order['paylog']['di_credit']].'抵扣';
			$order['price']=$order['price']-$order['paylog']['di_fee'];
		}
		if($order['paytype']){
			$order['paytypetitle']=get_pay_types()[$order['paytype']];
		}
		//手动改价
		$order['oldprice']=round($order['goodsprice'] + $order['dispatchprice'] + $order['out_upfee'] - $order['level_de_price'] - $order['luck_de_price']-$order['buyxiu_de_price']  - $order['enough_de_price'] - $order['deductcredit2']-$order['coupon_de_price']-$order['deductcredit'],2);
		$order['up_price']=intval(($order['price']-$order['oldprice'])*100)/100;
		$return['order']=$order;
		$return['coupon_wait']=$this->model('task')->pop_coupon_wait();
		$return['coupon_wait']=$return['coupon_wait']?$return['coupon_wait'][0]:false;
		if(!$return['coupon_wait']){
				$return['coupon_hd']=$this->model('task')->pop_coupon_hd();
				if(!$return['coupon_hd']){
					$return['game_wait']=$this->model('task')->pop_game();
				}
		}
		//拼团
		$return['go_goodsitem']=$order['is_team']?'jy_pingdd':'jy_weishop';
		$return['post']=array('libaos'=>'');
		return $return;
	}
}